#include "myline.h"
#include <QPainter>
#include <QtMath>
#include <QDebug>
#include <QGraphicsSceneMouseEvent>

MyLine::MyLine()
{
    this->setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable);
}

QRectF MyLine::boundingRect() const
{
    qreal l,b,w,h;
    QPointF cenPt((mspt.x()+mept.x())/2.0,(mspt.y()+mept.y())/2.0);
    w = fabs(mept.x()-mspt.x());
    h = fabs(mept.y()-mspt.y());
    l = cenPt.x()-w/2;
    b = cenPt.y()-h/2;
    return QRectF(l,b,w,h).adjusted(-6,-6,6,6);
}

void MyLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->drawLine(mspt,mept);
    if(this->isSelected())			// 当图元被选中后，需要显示3个小的特征方框
    {
        QPen pen(Qt::DashLine);
        painter->setPen(pen);
        //painter->drawRect(this->boundingRect());
        DrawSnapRect(painter,mspt);
        DrawSnapRect(painter,mept);
        DrawSnapRect(painter,QPointF((mspt.x()+mept.x())/2.0,(mspt.y()+mept.y())/2.0));
    }
}

// 专门为了显示小方框定义的绘图函数，被paint所调用
void MyLine::DrawSnapRect(QPainter *painter,QPointF cpt)
{
    QBrush brush(Qt::blue);		//  定义成蓝色，
    painter->setBrush(brush);		//  带笔刷的填充
    painter->drawRect(cpt.x()-5,cpt.y()-5,10,10);	// 方框宽度为10
}

int LineChoice= 0;
void MyLine::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        LineChoice = 0;
        QPointF curpt =  event->scenePos();
        // 为了捕捉到直线的3个特征点，我们需要比较拾取点与这3个点之间的距离，这里直接输入5为距离
        qreal dist;
        dist = sqrt(pow((mspt.x()-curpt.x()),2)+pow((mspt.y()-curpt.y()),2));
        if(dist < 5)
        {
            LineChoice = 1;   // 捕捉到直线的开始点
            return;
        }
        dist = sqrt(pow((mept.x()-curpt.x()),2)+pow((mept.y()-curpt.y()),2));
        if(dist < 5)
        {

            LineChoice = 2;   // 捕捉到直线的终止点
            return;
        }
        QPointF cenpt((mspt.x()+mept.x())/2.0,(mspt.y()+mept.y())/2.0);
        dist = sqrt(pow((cenpt.x()-curpt.x()),2)+pow((cenpt.y()-curpt.y()),2));
        if(dist < 5)
        {
            LineChoice = 3;   // 捕捉到直线的中间点
            return ;
        }
        QGraphicsItem::mousePressEvent(event);
    }
}

    void MyLine::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    {
        if(LineChoice== 1)
            mspt = event->scenePos();       //  直线的开始点直接移动到鼠标点
        if(LineChoice== 2)
            mept = event->scenePos();       //  直线的终止点直接移动到鼠标点
        if(LineChoice== 3)                   //  捕捉到中间点，可以平移该直线
        {
            QPointF cenpt((mspt.x()+mept.x())/2.0,(mspt.y()+mept.y())/2.0);
            qreal dx = event->scenePos().x()-cenpt.x();
            qreal dy = event->scenePos().y()-cenpt.y();
            mspt += QPointF(dx,dy);
            mept += QPointF(dx,dy);
            //this->moveBy(dx,dy);
        }
        this->update();
    }

    QPainterPath MyLine::shape() const
    {
        QPainterPath path;

        qreal angle = atan((mept.y()-mspt.y())/(mept.x()-mspt.x()));	// 先计算出直线的倾斜弧度值angle，
        qreal angle1 = angle-M_PI*85/180.0;								// 再把这个弧度顺时针转85度弧度
        qreal angle2 = angle+M_PI*85/180.0;							// 逆时针转85度的弧度值

        QPointF p1,p2,p3,p4;				// 定义4个点，分别是直线包围框的4个角点
        p1 = mspt+QPointF(3*cos(angle1),3*sin(angle1));
        p2 = mspt+QPointF(3*cos(angle2),3*sin(angle2));
        p3 = mept+QPointF(3*cos(angle2),3*sin(angle2));
        p4 = mept+QPointF(3*cos(angle1),3*sin(angle1));

        // 下面的代码用来围绕包围框形成一个路径
        path.moveTo(p1);
        path.lineTo(p2);
        path.lineTo(p3);
        path.lineTo(p4);
        path.lineTo(p1);
        return path;
    }
